package com.catmiw.wyz;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.catmiw.wyz.util.ExcelUtils;
import com.catmiw.wyz.util.NetUtils;

public class ChineseTrainning {
	public static String XLSFILENAME = "D:/王奕卓的教育/基础学习训练/语文生字表.xls";
	public static String WORDGROUPFILENAME = "D:/王奕卓的教育/基础学习训练/汉字基础词组库.txt";
	public static String PINYINCACHEFILENAME = "D:/王奕卓的教育/基础学习训练/词组拼音库.txt";
	

	public static Map<String, String> PINYINCACHE = null;

	public static List<String> loadWordgroup() {
		List<String> wgs = new ArrayList<String>();
		BufferedReader fi = null;

		try {
			fi = new BufferedReader(new FileReader(WORDGROUPFILENAME));
			String temp = null;
			while ((temp = fi.readLine()) != null) {
				temp=new String(temp.getBytes(),"UTF-8");
				System.out.println(temp);
				wgs.add(temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return wgs;
	}

	public static void writePinyinCache(String cizu, String pinyin) {
		PINYINCACHE.put(cizu, pinyin);
		FileWriter fw = null;
		try {
			fw = new FileWriter(PINYINCACHEFILENAME, true);
			fw.append(cizu).append("\t").append(pinyin).append("\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void loadPinyinCache() {
		PINYINCACHE = new HashMap<String, String>();
		BufferedReader fi = null;
		try {
			fi = new BufferedReader(new FileReader(PINYINCACHEFILENAME));
			String temp = null;
			while ((temp = fi.readLine()) != null) {
				if (!(temp.trim().equals(""))) {
					String[] tt = temp.split("\t");
					if (tt.length == 2) {
						PINYINCACHE.put(tt[0], tt[1]);
					} else if (tt.length == 1) {
						PINYINCACHE.put(tt[0], "");
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static String getPinyin(String str) {
		if (PINYINCACHE == null) {
			loadPinyinCache();
		}
		if (PINYINCACHE.keySet().contains(str)) {
			return PINYINCACHE.get(str);
		}
		System.out.println("正在网络查询[" + str + "]的拼音...");
		NETSEARCHETIMES++;
		HttpClient httpclient = new DefaultHttpClient();

		String py = null;
		try {
			String url = "http://www.iciba.com/"
					+ URLEncoder.encode(str, "utf-8");
			String responseBody = NetUtils.getResponseFromNet(url);
			int pos = 0;
			while (responseBody.indexOf("<strong>[", pos + 1) > 0) {
				pos = responseBody.indexOf("<strong>[", pos + 1);
				int pos2 = responseBody.indexOf("</strong>", pos + 1);
				py = responseBody.substring(pos + 9, pos2 - 1);
			}
			if (py != null) {
				writePinyinCache(str, py);
			} else {
				writePinyinCache(str, "");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}

		return py;
	}

	public static List<Character>[] getCharsList(int unit) {
		List<Character>[] chars = new ArrayList[] { new ArrayList<Character>(),
				new ArrayList<Character>(), new ArrayList<Character>() };
		Workbook wb = ExcelUtils.getWorkbook(XLSFILENAME);
		Sheet sheet = wb.getSheet("生字表");
		int count = 0;

		for (Row row : sheet) {
			if (count++ == 0)
				continue;
			String temp = null;
			char[] cs = null;
			Cell cell = row.getCell(0);
			int iunit = new Double(cell.getNumericCellValue()).intValue();
			if (iunit > unit)
				continue;
			cell = row.getCell(2);
			if (cell != null) {
				temp = cell.getStringCellValue();
				cs = temp.toCharArray();
				for (char c : cs) {
					if (!chars[0].contains(c))
						chars[0].add(c);
				}
			}
			cell = row.getCell(1);
			if (cell != null) {
				temp = cell.getStringCellValue();
				cs = temp.toCharArray();
				for (char c : cs) {
					chars[1].add(c);
				}
			}
			cell = row.getCell(3);
			if (cell != null) {
				temp = cell.getStringCellValue();
				cs = temp.toCharArray();
				for (char c : cs) {
					chars[2].add(c);
				}
			}
		}
		for (int i = chars[1].size() - 1; i >= 0; i--) {
			Character c = chars[1].get(i);
			if (chars[0].contains(c)) {
				chars[1].remove(i);
			}
		}

		for (int i = chars[2].size() - 1; i >= 0; i--) {
			Character c = chars[2].get(i);
			if (chars[0].contains(c)) {
				chars[2].remove(i);
			} else if (chars[1].contains(c)) {
				chars[2].remove(i);
			}
		}

		return chars;
	}

	public static Random RAND = new Random();

	public static int NETSEARCHETIMES = 0;

	/**
	 * 根据拼音写汉字
	 */
	public static Map<String, String> createPinyin(List<Character>[] chars,
			List<String> wgs, int count) {
		Map<String, String> map = new HashMap<String, String>();
		Map<String, String> result = new HashMap<String, String>();

		while (result.size() < count) {
			System.out.println(result.size()+":"+count);
			int rand = RAND.nextInt(chars[0].size());
			Character c = chars[0].get(rand);
			// Character c = '千';
			if (result.containsKey(c))
				continue;
			System.out.println("选中:" + c);
			NETSEARCHETIMES = 0;
			map.clear();
			for (String temp : wgs) {
				if (temp.contains("" + c)) {
					boolean allknow = true;
					for (char cc : temp.toCharArray()) {
						if (!chars[0].contains(cc)) {
							allknow = false;
							break;
						}
					}
					if (allknow) {
						String pyy = getPinyin(temp);
						if (pyy != null && !"".equals(pyy)) {
							map.put(temp, pyy);
						}
						if (NETSEARCHETIMES >= 5)
							break;
					}
				}
			}
			if (map.size() > 0) {
				rand = RAND.nextInt(map.size());
				Set<String> key = map.keySet();
				Object tt[] = (key.toArray());
				String tt2 = (String) (tt[rand]);
				result.put(tt2, map.get(tt2));
				//System.out.println(tt2 + "\t" + map.get(tt2));
			}
		}

		return result;
	}

	public static List<Character> createTingxie(List<Character>[] chars,
			int difficult, int count,String date,Workbook wb) {
		List<Character> result = new ArrayList<Character>();
		Sheet sheet=wb.getSheet("不会写的字");
		List<Character> errors= loadError(sheet, date);
		for (Character cc:errors){
			if (result.size() < count)
				result.add(cc);
		}
		while (result.size() < count) {
			int rand = RAND.nextInt(chars[difficult].size());
			Character c = chars[difficult].get(rand);
			if (!result.contains(c)) {
				result.add(c);
			}
		}
		return result;
	}

	public static List<String> createZhuyin(List<Character>[] chars,
			List<String> wgs, int difficult, int length, int count,String date,Workbook wb) {
		if (difficult < 0 || difficult > 2)
			throw new RuntimeException("无效的难度系数：" + difficult);
		List<String> result = new ArrayList<String>();
		Sheet sheet=wb.getSheet("不会读的字");
		List<Character> errors= loadError(sheet, date);

		List<Character> ccd = new ArrayList<Character>();
		List<String> ttt = new ArrayList<String>();

		for (int i = 0; i <= difficult; i++) {
			for (Character c : chars[i]) {
				ccd.add(c);
			}
		}

		System.out.println("共" + ccd.size() + "个字");

		while (result.size() < count) {
			int rand = RAND.nextInt(ccd.size());
			Character c =null;
			if (errors.size()>0){
				c=errors.get(0);
				errors.remove(0);
			} else {
				c = ccd.get(rand);
			}
			// Character c='拖';
			//System.out.println("选中:" + c);
			ttt.clear();
			for (String temp : wgs) {
				if (temp.contains("" + c) && temp.length() <= length) {
					boolean allknow = true;
					for (char cc : temp.toCharArray()) {
						if (!ccd.contains(cc)) {
							allknow = false;
							break;
						}
					}
					if (allknow) {
						ttt.add(temp);
					}
				}
			}
			if (ttt.size() > 0) {
				rand = RAND.nextInt(ttt.size());
				result.add(result.size()==0?0:RAND.nextInt(result.size()),ttt.get(rand));
				//System.out.println(ttt.get(rand));
			}
		}

		return result;
	}

	public static List<Character> createZuci(List<Character>[] chars,
			List<String> wgs, int difficult, int length, int count) {
		if (difficult < 0 || difficult > 2)
			throw new RuntimeException("无效的难度系数：" + difficult);
		List<Character> result = new ArrayList<Character>();

		List<Character> ccd = new ArrayList<Character>();
		List<String> ttt = new ArrayList<String>();

		for (int i = 0; i <= difficult; i++) {
			for (Character c : chars[i]) {
				ccd.add(c);
			}
		}
		System.out.println("共" + ccd.size() + "个字");

		while (result.size() < count) {
			int rand = RAND.nextInt(ccd.size());
			Character c = ccd.get(rand);
			ttt.clear();

			for (String temp : wgs) {
				if (temp.contains("" + c) && temp.length() <= length) {
					boolean allknow = true;
					for (char cc : temp.toCharArray()) {
						if (!ccd.contains(cc)) {
							allknow = false;
							break;
						}
					}
					if (allknow) {
						ttt.add(temp);
					}
				}
			}
			if (ttt.size() >= 10) {
				result.add(c);
			}
		}

		return result;
	}

	public static Map<Character, List<Character>> countBushou(
			List<Character>[] chars, Map<Character, Character> bushou,
			int difficult) {
		if (difficult < 0 || difficult > 2)
			throw new RuntimeException("无效的难度系数：" + difficult);
		Map<Character, List<Character>> result = new HashMap<Character, List<Character>>();

		for (int i = 0; i <= difficult; i++) {
			for (Character c : chars[i]) {
				Character b = bushou.get(c);
				if (b == null)
					continue;
				List<Character> list = result.get(b);
				if (list == null)
					list = new ArrayList<Character>();
				list.add(c);
				result.put(b, list);
			}
		}
		return result;
	}

	public static Map<Character, Character> loadBushou() {
		Map<Character, Character> result = new HashMap<Character, Character>();
		Workbook wb = ExcelUtils.getWorkbook(XLSFILENAME);
		Sheet sheet = wb.getSheet("部首");

		for (Row row : sheet) {
			for (int i = 0; i < 5; i++) {
				Cell cell = row.getCell(i * 2);
				if (cell == null)
					continue;
				String temp = cell.getStringCellValue();
				cell = row.getCell(i * 2 + 1);
				if (cell == null)
					continue;
				String temp2 = cell.getStringCellValue();
				result.put(temp.charAt(0), temp2.charAt(0));
			}
		}

		return result;
	}

	public static int BUSHOUZISHU = 8;

	public static List<Character> createBushou(List<Character>[] chars,
			int difficult, int count) {
		List<Character> result = new ArrayList<Character>();

		Map<Character, Character> bs = loadBushou();
		Map<Character, List<Character>> r = countBushou(chars, bs, difficult);
		Set<Character> key = r.keySet();
		for (Character c : key) {
			if (r.get(c).size() >= BUSHOUZISHU)
				result.add(c);
		}

		while (result.size() > count) {
			int rand = RAND.nextInt(result.size());
			result.remove(rand);
		}

		return result;

	}

	public static void addSubtitle(String title, Sheet sheet, Row row,
			CellStyle style, int rowcount) {
		Cell cell = row.createCell(0);
		cell.setCellValue(title);
		cell.setCellStyle(style);
		for (int j = 1; j < 20; j++) {
			cell = row.createCell(j);
			cell.setCellStyle(style);
		}
		sheet.addMergedRegion(new CellRangeAddress(rowcount - 1, rowcount - 1,
				0, 19));
	}

	public static void createTestPaper(int unit, int tingxiecount,
			int pinyincount, int zhuyincount, int zucicount, int bushoucount) {
		String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
		createTestPaper(date, unit, tingxiecount, pinyincount, zhuyincount,
				zucicount, bushoucount);
	}

	public static void createTestPaper(String sheetname, int unit,
			int tingxiecount, int pinyincount, int zhuyincount, int zucicount,
			int bushoucount) {
		System.out.println("开始产生试卷。。。"+sheetname);
		Workbook wb = ExcelUtils.getWorkbook(XLSFILENAME);

		Font font = wb.createFont();
		font.setFontHeightInPoints((short) 12);
		font.setFontName("微软雅黑");

		CellStyle style = wb.createCellStyle();
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setBorderRight(CellStyle.BORDER_THIN);
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setFont(font);

		CellStyle style2 = wb.createCellStyle();
		style2.cloneStyleFrom(style);
		style2.setAlignment(CellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

		Sheet sheet = wb.getSheet(sheetname + "试卷");
		if (sheet == null)
			sheet = wb.createSheet(sheetname + "试卷");
		Sheet sheet2 = wb.getSheet(sheetname + "答案");
		if (sheet2 == null)
			sheet2 = wb.createSheet(sheetname + "答案");
		wb.setSheetOrder(sheet.getSheetName(), 4);
		wb.setSheetOrder(sheet2.getSheetName(), 5);
		wb.setActiveSheet(2);
		sheet.setSelected(true);
		
		List<Character>[] chars = getCharsList(unit);
		List<String> wgs = loadWordgroup();

		for (int i = 0; i < 10; i++) {
			sheet.setColumnWidth(i * 2, 925);
			sheet.setColumnWidth(i * 2 + 1, 1375);
			sheet2.setColumnWidth(i * 2, 925);
			sheet2.setColumnWidth(i * 2 + 1, 1375);
		}

		// 产生听写的内容
		System.out.println("1、产生听写的内容"+sheetname);
		int rowcount = 0;
		int rowcountb = 0;

		List<Character> tingxie = createTingxie(chars, 0, tingxiecount,sheetname,wb);
		Row row = sheet.createRow(rowcount++);
		row.setHeight((short) 600);
		Row row2 = sheet2.createRow(rowcountb++);
		row2.setHeight((short) 600);

		Cell cell = null;
		addSubtitle(sheetname + "语文基础测试", sheet, row, style2, rowcount);
		addSubtitle(sheetname + "语文基础测试", sheet2, row2, style2, rowcountb);

		row = sheet.createRow(rowcount++);
		row.setHeight((short) 500);
		addSubtitle("    一、 听写", sheet, row, style, rowcount);

		row2 = sheet2.createRow(rowcountb++);
		row2.setHeight((short) 500);
		addSubtitle("    一、 听写", sheet2, row2, style, rowcountb);

		for (int i = 0; i < tingxiecount / 10; i++) {
			row = sheet.createRow(rowcount++);
			row.setHeight((short) 600);
			for (int j = 0; j < 10; j++) {
				cell = row.createCell(j * 2);
				cell.setCellValue(i * 10 + j + 1);
				cell.setCellStyle(style2);
				cell = row.createCell(j * 2 + 1);
				cell.setCellStyle(style2);
				// cell.setCellValue(""+tingxie.get(i*10+j));
			}
			row2 = sheet2.createRow(rowcountb++);
			row2.setHeight((short) 600);
			for (int j = 0; j < 10; j++) {
				cell = row2.createCell(j * 2);
				cell.setCellValue(i * 10 + j + 1);
				cell.setCellStyle(style2);
				cell = row2.createCell(j * 2 + 1);
				cell.setCellStyle(style2);
				cell.setCellValue("" + tingxie.get(i * 10 + j));
			}
		}

		// 第二项，看拼音写汉字
		System.out.println("二、 看拼音写汉字"+sheetname);
		Row rowb = null;
		row = sheet.createRow(rowcount++);
		row.setHeight((short) 500);
		addSubtitle("    二、 看拼音写汉字", sheet, row, style, rowcount);

		row2 = sheet2.createRow(rowcountb++);
		row2.setHeight((short) 500);
		addSubtitle("    二、 看拼音写汉字", sheet2, row2, style, rowcountb);

		Map<String, String> map = createPinyin(chars, wgs, pinyincount);
		Set<String> set = map.keySet();
		int icount1 = 0;
		for (String s : set) {
			if (icount1++ % 5 == 0) {
				row = sheet.createRow(rowcount++);
				row.setHeight((short) 500);
				rowb = sheet2.createRow(rowcountb++);
				rowb.setHeight((short) 500);
				row2 = sheet.createRow(rowcount++);
				row2.setHeight((short) 600);
			}
			cell = row.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell.setCellValue(map.get(s));
			cell = rowb.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell.setCellValue(s);
			cell = row2.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style);
			for (int i = 1; i < 4; i++) {
				cell = row.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
				cell = rowb.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
				cell = row2.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
			}
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 2,
					rowcount - 2, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
			sheet2.addMergedRegion(new CellRangeAddress(rowcountb - 1,
					rowcountb - 1, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 1,
					rowcount - 1, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
		}

		// 第三项，汉字注音
		System.out.println("3、汉字注音"+sheetname);
		row = sheet.createRow(rowcount++);
		row.setHeight((short) 500);
		addSubtitle("    三、 汉字注音", sheet, row, style, rowcount);

		List<String> zhuyin = createZhuyin(chars, wgs, 2, 2, zhuyincount,sheetname,wb);
		icount1 = 0;
		for (String s : zhuyin) {
			if (icount1++ % 5 == 0) {
				row = sheet.createRow(rowcount++);
				row.setHeight((short) 500);
				row2 = sheet.createRow(rowcount++);
				row2.setHeight((short) 600);
			}
			cell = row.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell = row2.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell.setCellValue(s);
			for (int i = 1; i < 4; i++) {
				cell = row.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style2);
				cell = row2.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style2);
			}
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 2,
					rowcount - 2, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 1,
					rowcount - 1, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
		}

		// 第四、组词
		System.out.println("四、 汉字组词（组2个词）"+sheetname);
		row = sheet.createRow(rowcount++);
		row.setHeight((short) 500);
		addSubtitle("    四、 汉字组词（组2个词）", sheet, row, style, rowcount);

		List<Character> zuci = createZuci(chars, wgs, 0, 2, zucicount);
		icount1 = 0;
		for (Character s : zuci) {
			if (icount1++ % 5 == 0) {
				row = sheet.createRow(rowcount++);
				row.setHeight((short) 500);
				row2 = sheet.createRow(rowcount++);
				row2.setHeight((short) 600);
			}
			cell = row.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell.setCellValue("" + s);
			cell = row2.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style);
			for (int i = 1; i < 4; i++) {
				cell = row.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
				cell = row2.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
			}
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 2,
					rowcount - 2, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 1,
					rowcount - 1, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
		}

		// 第五、部首组字
		System.out.println("五、 部首组字（组2个字）"+sheetname);
		row = sheet.createRow(rowcount++);
		row.setHeight((short) 500);
		addSubtitle("    五、 部首组字（组2个字）", sheet, row, style, rowcount);

		List<Character> bushou = createBushou(chars, 1, bushoucount);
		//System.out.println("共" + bushou.size() + "个部首");
		icount1 = 0;
		for (Character s : bushou) {
			if (icount1++ % 5 == 0) {
				row = sheet.createRow(rowcount++);
				row.setHeight((short) 500);
				row2 = sheet.createRow(rowcount++);
				row2.setHeight((short) 600);
			}
			cell = row.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style2);
			cell.setCellValue("" + s);
			cell = row2.createCell((icount1 - 1) % 5 * 4);
			cell.setCellStyle(style);
			for (int i = 1; i < 4; i++) {
				cell = row.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
				cell = row2.createCell((icount1 - 1) % 5 * 4 + i);
				cell.setCellStyle(style);
			}
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 2,
					rowcount - 2, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
			sheet.addMergedRegion(new CellRangeAddress(rowcount - 1,
					rowcount - 1, (icount1 - 1) % 5 * 4,
					(icount1 - 1) % 5 * 4 + 3));
		}

		ExcelUtils.writeWorkbook(wb, XLSFILENAME);

	}

	public static List<Character> loadError(Sheet sheet, String date) {
		List<Character> result = new ArrayList<Character>();

		if (sheet == null)
			return result;
		String c = null;
		int d = 0, passtimes,testtimes = 0;
		for (Row row : sheet) {
			c = (row.getCell(0) == null ? "" : row.getCell(0)
					.getStringCellValue());
			if ("".equals(c) || c.length() != 1)
				continue;
			d = new Double(row.getCell(1) == null ? 20100101 : row.getCell(1)
					.getNumericCellValue()).intValue();
			testtimes = new Double(row.getCell(2) == null ? 0 : row.getCell(2)
					.getNumericCellValue()).intValue();
			passtimes = new Double(row.getCell(3) == null ? 0 : row.getCell(3)
					.getNumericCellValue()).intValue();

			try {
				Cell cell=null;
				Date date1 = DateUtils.parseDate("" + d,
						EnglishTrainning.DATEFORMAT);

				int adddays=0;
				if (passtimes==0){
					adddays=-1;
				} else {
					adddays=(int)(EnglishTrainning.ERRORTESTPERIOD*Math.pow(passtimes/testtimes,
						EnglishTrainning.ERRORRATIOPOW)*Math.pow(testtimes,EnglishTrainning.TESTTIMESPOW));
				}
				
				date1 = DateUtils.addDays(date1, adddays);
				Date date2 = DateUtils.parseDate(date,
						EnglishTrainning.DATEFORMAT);
				if (date2.compareTo(date1) > 0) {
					result.add(c.charAt(0));
					cell=row.getCell(2);
					cell.setCellValue(testtimes+1);
					cell=row.getCell(1);
					cell.setCellValue(Integer.parseInt(date));
					cell=row.createCell(4);
					cell.setCellValue("√");
					continue;
				}
				//System.out.println(">"+row.getCell(4).getStringCellValue());
				row.createCell(4).setCellValue("");
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		createTestPaper(216, 30, 30, 40, 5, 5);
		/*
		 * List<Character>[] lcs=getCharsList(216); List<Character> cs=new
		 * ArrayList<Character>(); for (int i=0;i<3;i++){ for (Character
		 * c:lcs[i]){ cs.add(c); } }
		 * 
		 * for (int i=0;i<cs.size();i++){ System.out.print(cs.get(i)); if
		 * (i%100==99) System.out.println(); }
		 */
		/*
		 * Map<Character,Character> bs=loadBushou(); List<Character>[]
		 * lcs=getCharsList(216); Map<Character,List<Character>>
		 * r=countBushou(lcs,bs,1); Set<Character> key=r.keySet(); for
		 * (Character c:key){ if (r.get(c).size()>=1)
		 * System.out.println(c+":"+r.get(c)); }
		 * 
		 * List<Character> rr=createBushou(lcs,0,5); System.out.println(rr);
		 */
	}

}
